$(document).ready(function(){
	getServiceVip();
});

function getServiceVip(){
	$('#table_list').DataTable({
        dom: '<"top">rt<"bottom"flip><"clear">',
        language: {
            "sUrl":'../static/plugins/datatables/language.json'
        },
        "searching": false,
        "processing": true,
        "serverSide": true,
        ajax:{
            url:'/service-vip/get-service-vip',
            type:'post',
            data:function(d){
            	d.service_type = $('#service_type').val();
            	d.status = $('#status').val();
            },
            error:getError
        },
        aoColumns: [
            { "data": "create_time", "bSortable": false},
            { "data": "service_type_name", "bSortable": false },
            { "data": "home_time", "bSortable": false },
            { "data": "cust_name", "bSortable": false },
            { "data": "cust_tel", "bSortable": false },
            { "data": "service_demand", "bSortable": false },
            { "data": "status", "bSortable": false },
            { "data": "id", "bSortable": false}
        ],
        columnDefs: [
            {
                "targets": [6],
                "data": "use_flg",
                "render": function(data, type, full) {
                	return html = '<span class="switch" data-id="'+ full.id +'" data-status="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
                }
            },
            {
                "targets": [7],
                "orderable":false,
                "searchable":false,
                "render": function(data, type, full) {
                    return '<a href="javascript:" onclick="getOperLog('+ full.id +')">操作记录</a>';
                }
            },
        ]
    });
}

function getOperLog(id){
	$.post('/service-vip/get-service-vip-log',{id:id},success,'json');
	return false;
	function success(data){
		if(data.code == 200){
			$('#service-vip-log-modal').modal('show');
			var result = data.val;
			var html = '<label for="tag_group_id_add">联系记录</label>';
			if(result.length > 0){
				result.forEach(function(e,index){
					var style = 'border-bottom:2px solid #999;';
					if(index == result.length-1){
						style = '';
					}
					html += '<div class="col-sm-12 control-label" style="'+style+'">';
					html += '<p>'+e.oper_time+'</p>';
					html += '<p>'+e.record+'</p>';
					html += '</div>';
				});
			}
			$('#service-vip-log-modal').find('input[name=service_vip_id]').val(id);
			$('#service-vip-log-modal').find('.old_service_log').html(html);
			$('#record').val('');
		}else{
        	$.jBox.error(data.val, '错误');
        }
	}
}

function getServiceVipDetail(id){
	$.post('/service-vip/get-service-vip-detail',{id:id},success,'json');
	$('#service-vip-modal').modal('show');
	return false;
	function success(data){
    	if(data.code == 200){
            var r = data.val;
            var form_id = $('#service_vip_form');
            $('#service_vip_id').val(id);
    		form_id.find('.service_type_name').text(r.service_type_name);
    		form_id.find('.create_time').text(r.create_time);
    		form_id.find('.service_code').text(r.service_code);
    		form_id.find('.cust_name').text(r.cust_name);
    		form_id.find('.cust_tel').text(r.cust_tel);
    		form_id.find('#sys_memo').val(r.sys_memo);
//    		form_id.find('.province_name').text(r.province_name);
//    		form_id.find('.city_name').text(r.city_name);
//    		form_id.find('.area_name').text(r.area_name);
//    		form_id.find('.address').text(r.address);
    		var html = '';
    		switch (r.service_type){
    			case '1':
    				html += '<div class="form-group">'+
    							'<label for="tag_group_id_add">居家面积：</label><span class="home_area">'+r.home_area+'</span>'+
    						'</div>'+
    						'<div class="form-group">'+
    							'<label for="tag_group_id_add">希望送达时间：</label><span class="home_time">'+r.home_time+'</span>'+
    						'</div>'+
    						'<div class="form-group">'+
								'<label for="tag_group_id_add">服务需求：</label><span class="service_demand">'+r.service_demand+'</span>'+
							'</div>';
    				break;
    			case '2':
    				html += '<div class="form-group">'+
								'<label for="tag_group_id_add">CRM服务项目：</label><span class="service_project">'+r.service_project+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">希望送达时间：</label><span class="home_time">'+r.home_time+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">服务需求：</label><span class="service_demand">'+r.service_demand+'</span>'+
							'</div>';
    				break;
    			case '3':
    				html += '<div class="form-group">'+
								'<label for="tag_group_id_add">服务范围：</label><span class="service_range">'+r.service_range+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">到家检测时间：</label><span class="home_time">'+r.home_time+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">服务需求：</label><span class="service_demand">'+r.service_demand+'</span>'+
							'</div>';
    				break;
    			case '4':
    				html += '<div class="form-group">'+
								'<label for="tag_group_id_add">CRM服务项目：</label><span class="service_project">'+r.service_project+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">方便联系时间：</label><span class="home_time">'+r.home_time+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">服务需求：</label><span class="service_demand">'+r.service_demand+'</span>'+
							'</div>';
    				break;
    			case '5':
    				html += '<div class="form-group">'+
								'<label for="tag_group_id_add">配画需求空间：</label><span class="service_range">'+r.service_range+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">喜爱风格：</label><span class="cust_style">'+r.cust_style+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">预计需求日期：</label><span class="home_time">'+r.home_time+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">服务需求：</label><span class="service_demand">'+r.service_demand+'</span>'+
							'</div>';
					break;
    			case '6':
    				html += '<div class="form-group">'+
								'<label for="tag_group_id_add">目的地：</label><span class="destination">'+r.destination+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">旅程伙伴：</label><span class="trip_partner">'+r.trip_partner+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">旅程预计人数：</label><span class="partner_count">'+r.partner_count+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">旅程偏好：</label><span class="trip_preference">'+r.trip_preference+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">旅程天数：</label><span class="trip_day">'+r.trip_day+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">旅程预算：</label><span class="trip_budget">'+r.trip_budget+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">服务需求：</label><span class="service_demand">'+r.service_demand+'</span>'+
							'</div>';
					break;
    			case '7':
    				html += '<div class="form-group">'+
								'<label for="tag_group_id_add">欲想举办主题：</label><span class="cust_style">'+r.cust_style+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">目的：</label><span class="service_project">'+r.service_project+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">预计活动时间：</label><span class="home_time">'+r.home_time+'</span>'+
							'</div>'+
							'<div class="form-group">'+
								'<label for="tag_group_id_add">举办party备注信息：</label><span class="party_memo">'+r.party_memo+'</span>'+
							'</div>';
    				break;
    		}
//    		form_id.find('.service_type').html(html);
        }else{
        	$.jBox.error(data.val, '错误');
        }
    }
}

function table_list_reload(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
}

function changeServiceVipStatus(id,status){
	title = '确定禁用此vip服务信息吗？';
	if(status == 1){
		title = '确定启用此vip服务信息吗？';
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/service-vip/change-service-vip-status',{id:id,status:status},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
            $.jBox.close();
            if(status == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
        }
    });
}

//重置操作
$('#reset_button').click(function(){
    $('#service_type').val('');
    $('#status').val(0);
    table_list_reload();
});

$('#table_list').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var status = $(this).parent().attr('data-status');
	changeServiceVipStatus(id,status);
});

$('#service-vip-modal').on('click','.ajax_post_tag',function(){
	var form = $('#service_vip_form');
	$.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#service-vip-modal').modal('hide');
                window.setTimeout(function () { table_list_reload(); }, 1000);
            }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
	}
});

$('#service-vip-log-modal').on('click','.ajax_post_tag',function(){
	var form = $('#service_log_vip_form');
	if(form.find('#record').val() == ''){
		$.jBox.error('请填写联系记录', '错误');
		return false;
	}
	$.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#service-vip-log-modal').modal('hide');
                table_list_reload();
            }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
	}
});

function exportServiceVip(){
	var form = $('#service-vip-select');
	form.submit();
}